<?php

namespace app\api\controller;

use app\api\model\GroupApply;
use app\api\model\GroupUser;
use app\common\controller\Api;

/**
 * 群组模块
 */
class Group extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    /**
     * 设置群管理
     * group_id  群组ID
     * user_id   需要设置为管理的用户ID
     * @throws \think\exception\DbException
     */
    public function groupManagement()
    {
        $group_id = input('group_id');
        $user['user_id'] = $this->uid;
        $user['group_id'] = $group_id;
        $group = GroupUser::get($user);
        if ($group['auth'] != 1) $this->error('您不是群主！');//判断是否是群主
        GroupUser::update(['auth' => 2], ['group_id' => $group_id, 'user_id' => input('user_id')]);
        $this->success('设置成功');
    }

    /**
     * 转让群主
     * group_id  群组ID
     * user_id   需要转让的用户ID
     * @throws \think\exception\DbException
     */
    public function groupHost()
    {
        $group_id = input('group_id');
        $user['user_id'] = $this->uid;
        $user['group_id'] = $group_id;
        $group = GroupUser::get($user);
        if ($group['auth'] != 1) $this->error('您不是群主！');//
        GroupUser::update(['auth' => 0], ['group_id' => $group_id, 'user_id' => $this->uid]);
        GroupUser::update(['auth' => 1], ['group_id' => $group_id, 'user_id' => input('user_id')]);
        $this->success('转让成功');
    }

    /**
     * 修改群简介
     * group_id  群组ID
     * introduction  简介
     * @throws \think\exception\DbException
     */
    public function updateIntroduction()
    {
        $param = input();
        if (empty($param['group_id']) || empty($param['introduction'])) $this->error('参数不全!');
        $user['user_id'] = $this->uid;
        $user['group_id'] = $param['group_id'];
        $group = GroupUser::get($user);
        if ($group && $group['auth'] != 0) {
            $this->success('修改成功', \app\api\model\Group::update(['introduction' => $param['introduction']], ['group_id' => $param['group_id']]));
        }
        $this->error('权限不足');
    }

    /**
     * 申请入群
     * group_id  群组ID
     * @throws \think\exception\DbException
     */
    public function apply()
    {
        $user['user_id'] = $this->uid;
        $user['group_id'] = input('group_id');
        if (GroupApply::get($user)) $this->error('您已申请');
        $this->success('申请成功', GroupApply::create($user));
    }

    /**
     * group_id 群组ID
     * 申请入群列表
     */
    public function applyList()
    {
        $this->success('成功', model('group_apply')->getList(input('group_id')));
    }

    /**
     * group_id  群组ID
     * user_id  拒绝入群的用户ID
     * 拒绝入群
     */
    public function groupRefuse()
    {
        $user['user_id'] = $this->uid;
        $user['group_id'] = input('group_id');
        $group = GroupUser::get($user);
        if ($group['auth'] == 0) $this->error('您没有权限！');//判断是否有权限
        $this->success('拒绝成功', GroupApply::destroy(['user_id' => input('user_id'), 'group_id' => input('group_id')]));
    }

    /**
     * group_id  群组ID
     * user_id  拒绝入群的用户ID
     * 修改群头像
     * @throws \think\exception\DbException
     */
    public function updateAvatar()
    {
        $user['user_id'] = $this->uid;
        $user['group_id'] = input('group_id');
        $group = GroupUser::get($user);
        if ($group['auth'] == 0) $this->error('您没有权限！');//判断是否有权限
        $this->success('修改成功', \app\api\model\Group::update(['avatar' => input('avatar')], ['group_id' => input('group_id')]));
    }

    /**
     *
     * 获取群详情
     */
    public function groupDetail()
    {
        if (!input('group_id')) $this->error('参数错误');
        $this->success('成功', model('group')->getDetail(input('group_id'), $this->uid));
    }

    /**
     *
     * 是否展示到附近群
     */
    public function updateStatus()
    {
        $this->success('成功', \app\api\model\Group::update(['status' => input('status')], ['group_id' => input('group_id')]));
    }
}